package com.jingyanzi.sys.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.sys.dao.SysMemberGradeMapper;
import com.jingyanzi.sys.dao.SysRightsMapper;
import com.jingyanzi.sys.domain.MemberGradeDO;
import com.jingyanzi.sys.domain.RightsDO;
import com.jingyanzi.sys.domain.query.MemberGradeQueryDO;
import com.jingyanzi.sys.domain.query.RightQueryDO;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.IMemberConfigService;
import com.jingyanzi.sys.entity.SysMemberGrade;
import com.jingyanzi.sys.entity.SysMemberGradeExample;
import com.jingyanzi.sys.entity.SysRights;
import com.jingyanzi.sys.entity.SysRightsExample;
import com.jingyanzi.sys.entity.SysRightsExample.Criteria;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("iMemberConfigService")
public class MemberConfigServiceImpl extends BaseServiceImpl implements IMemberConfigService {

	private static Logger logger = Logger.getLogger(MemberConfigServiceImpl.class);

	@Autowired
	private SysMemberGradeMapper memberGradeMapper;

	@Autowired
	private SysRightsMapper rightsMapper;

	@Override
	public ResultDO addRights(RightsDO rightsDO) {
		ResultDO result = new ResultSupport(true);
		if (rightsDO == null) {
			result.setSuccess(false);
			result.setErrorMsg("会员权益DO对象为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员权益DO对象为空");
			return result;
		}

		if (isNumberInValid(rightsDO.getBusinessId())
				|| isNumberInValid(rightsDO.getLevel())) {
			result.setSuccess(false);
			result.setErrorMsg("配置有问题");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员权益businessId、level不能为空");
			return result;
		}

		SysRights sysRights = new SysRights();

		BeanUtilsExtends.copyProperties(sysRights, rightsDO);
		sysRights.setCreatetime(new Date());
		sysRights.setIsDelete(1);
		sysRights.setModifitime(new Date());
		int insertSelective = rightsMapper.insert(sysRights);

		if (insertSelective == 0) {
			result.setSuccess(false);
			result.setErrorMsg("插入失败");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员权益插入失败");
			return result;
		}
		result.setSuccess(true);
		result.setModel(ResultDO.FIRST_MODEL_KEY, sysRights.getId());

		return result;
	}

	@Override
	public ResultDO removeRights(Integer id, String name) {

		ResultDO result = new ResultSupport(true);
		if (isNumberInValid(id)) {
			result.setSuccess(false);
			result.setErrorMsg("ID不能未空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " ID不能为空");
			return result;
		}

		SysRights sysRights = new SysRights();
		sysRights.setId(id);
		sysRights.setIsDelete(-1);
		sysRights.setModifitime(new Date());
		sysRights.setModifi(name);

		int updateByPrimaryKeySelective = rightsMapper.updateByPrimaryKeySelective(sysRights);

		if (updateByPrimaryKeySelective == 0) {
			result.setSuccess(false);
			result.setErrorMsg("删除失败");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员权益删除失败");
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, id);

		return result;
	}

	@Override
	public ResultDO modifiRights(RightsDO rightsDO) {
		ResultDO result = new ResultSupport(true);
		if (rightsDO == null || isNumberInValid(rightsDO.getId())) {

			result.setSuccess(false);
			result.setErrorMsg("异常");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员权益DO对象不能为空");
			return result;
		}
		SysRights rights = new SysRights();
		BeanUtilsExtends.copyProperties(rights, rightsDO);
		rights.setModifitime(new Date());

		int updateByPrimaryKeySelective = rightsMapper.updateByPrimaryKeySelective(rights);

		if (updateByPrimaryKeySelective == 0) {

			result.setSuccess(false);
			result.setErrorMsg("异常");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员权益修改失败");
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, rightsDO.getId());
		return result;
	}

	@Override
	public ResultDO getRights(Integer id) {
		ResultDO result = new ResultSupport(true);
		if (isNumberInValid(id)) {
			result.setSuccess(false);
			result.setErrorMsg("ID为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员权益ID为空");
			return result;
		}

		SysRights selectByPrimaryKey = rightsMapper.selectByPrimaryKey(id);

		if (selectByPrimaryKey == null) {
			result.setSuccess(false);
			result.setErrorMsg("未找到");
			logger.error(ResultDO.DB_NO_DATA_MSG + " 会员权益不存在 id=" + id);
			return result;
		}

		RightsDO rights = new RightsDO();

		BeanUtilsExtends.copyProperties(rights, selectByPrimaryKey);

		result.setModel(ResultDO.FIRST_MODEL_KEY, rights);
		return result;
	}

	@Override
	public ResultDO queryRights(RightQueryDO queryDO) {

		ResultDO result = new ResultSupport(true);
		List<SysRights> list = null;
		SysRightsExample example = new SysRightsExample();
		Criteria createCriteria = example.createCriteria();
		List<RightsDO> rightsDOs = null;
		createCriteria.andIsDeleteEqualTo(1);
		if (queryDO != null) {

			if (!StringUtils.isEmpty(queryDO.getName())) {
				createCriteria.andNameLike("%" + queryDO.getName() + "%");
			}
		}
		if (queryDO.isPage()) {
			example.setLimitByPage(queryDO.getStart() + "," + queryDO.getPageRows());
			long count = rightsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = rightsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ResultDO.QUERY_ERROR_MSG + " 会员权益查询失败 ");
		}

		rightsDOs = getRightsDOList(list);
		result.setModel(ResultDO.FIRST_MODEL_KEY, rightsDOs);

		return result;
	}

	@Override
	public ResultDO addGrade(MemberGradeDO gradeDO) {

		ResultDO result = new ResultSupport(true);
		if (gradeDO == null) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.PROPERTY_VALUE_SET_ERROR);
			result.setErrorMsg(ResultDO.PROPERTY_VALUE_SET_ERROR + " 会员等级DO对象为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员等级DO对象为空");
			return result;
		}

		SysMemberGrade sysMemberGrade = new SysMemberGrade();
		result = BeanUtilsExtends.copy(sysMemberGrade, gradeDO);
		if (!result.isSuccess()) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "会员等级对象转换失败");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级对象转换失败 ");
			return result;
		}

		sysMemberGrade.setCreatetime(new Date());
		sysMemberGrade.setIsDelete(1);
		sysMemberGrade.setModifitime(new Date());
		int insertSelective = memberGradeMapper.insert(sysMemberGrade);

		if (insertSelective == 0) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级添加失败");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级添加失败");
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, sysMemberGrade.getId());
		return result;
	}

	@Override
	public ResultDO removeGrade(Integer[] ids) {
		SysMemberGrade sysMemberGrade = new SysMemberGrade();
		ResultDO result = new ResultSupport(true);
		if (ids == null || ids.length == 0) {
			result.setSuccess(false);
			result.setErrorMsg("ID不能为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员等级ids为空");
			return result;
		}
		for (int i = 0; i < ids.length; i++) {
			sysMemberGrade.setId(ids[i]);
			sysMemberGrade.setIsDelete(-1);
			sysMemberGrade.setModifitime(new Date());

			int r = memberGradeMapper.updateByPrimaryKeySelective(sysMemberGrade);

			if (r < 0) {
				result.setSuccess(false);
			}
		}

		return result;
	}

	@Override
	public ResultDO modifiGrade(MemberGradeDO gradeDO) {
		
		ResultDO result = new ResultSupport(true);
		if (gradeDO == null || isNumberInValid(gradeDO.getId())) {
			result.setSuccess(false);
			result.setErrorMsg("ID不能为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员等级DO对象为空");
			return result;
		}
		SysMemberGrade sysMemberGrade = new SysMemberGrade();
		result = BeanUtilsExtends.copy(sysMemberGrade, gradeDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "会员等级对象转换失败");
			result.setSuccess(false);
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级对象转换失败 ");
			return result;
		}

		sysMemberGrade.setModifitime(new Date());

		int updateByPrimaryKeySelective = memberGradeMapper.updateByPrimaryKeySelective(sysMemberGrade);

		if (updateByPrimaryKeySelective == 0) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级修改失败");
			logger.error(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + " 会员等级修改失败");
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, gradeDO.getId());

		return result;
	}

	@Override
	public ResultDO getGrade(Integer id) {

		ResultDO result = new ResultSupport(true);
		if (isNumberInValid(id)) {
			result.setSuccess(false);
			result.setErrorMsg("ID不能为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 会员等级id为空");
			return result;
		}
		SysMemberGrade selectByPrimaryKey = memberGradeMapper.selectByPrimaryKey(id);

		if (selectByPrimaryKey == null) {
			result.setSuccess(false);
			result.setErrorMsg("查询为空");
			logger.error(ResultDO.DB_NO_DATA_MSG + " 会员等级数据不存在 id=" + id);
			return result;
		}
		MemberGradeDO memberGradeDO = getMemberGradeDO(selectByPrimaryKey);
		result.setModel(ResultDO.FIRST_MODEL_KEY, memberGradeDO);
		return result;
	}

	@Override
	public ResultDO queryGrade(MemberGradeQueryDO queryDO) {

		ResultDO result = new ResultSupport(true);

		List<SysMemberGrade> selectByExample = null;

		List<MemberGradeDO> memberGradeDOs = null;

		SysMemberGradeExample example = new SysMemberGradeExample();

		com.jingyanzi.sys.entity.SysMemberGradeExample.Criteria createCriteria = example.createCriteria();
		
		createCriteria.andIsDeleteEqualTo(1);

		if (queryDO.isPage()) {
			example.setLimitByPage(queryDO.getStart() + "," + queryDO.getPageRows());
			long count = memberGradeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			selectByExample = memberGradeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ResultDO.QUERY_ERROR_MSG + " 会员等级查询失败");
		}

		memberGradeDOs = getMemberGradeDOList(selectByExample);

		result.setModel(ResultDO.FIRST_MODEL_KEY, memberGradeDOs);

		return result;
	}

	@Override
	public ResultDO gradeCheckRepeat(MemberGradeDO gradeDO) {
		
		ResultDO result = new ResultSupport(true);
		
		SysMemberGradeExample example = new SysMemberGradeExample();
		
		com.jingyanzi.sys.entity.SysMemberGradeExample.Criteria createCriteria1 = example.createCriteria();
		com.jingyanzi.sys.entity.SysMemberGradeExample.Criteria createCriteria2 = example.createCriteria();
		
		// 根据等级名称、等级code进行查重
		// 如果有id，则查询此id意外的名称、code是否有重复
		if(!StringUtils.isEmpty(gradeDO.getName())) {
			createCriteria1.andNameEqualTo(gradeDO.getName());
			createCriteria1.andIsDeleteEqualTo(1);
			if(!isNumberInValid(gradeDO.getId())) {
				createCriteria1.andIdNotEqualTo(gradeDO.getId());
			}
			example.or(createCriteria1);
		}
		if(!StringUtils.isEmpty(gradeDO.getCode())) {
			createCriteria2.andCodeEqualTo(gradeDO.getCode());
			createCriteria2.andIsDeleteEqualTo(1);
			if(!isNumberInValid(gradeDO.getId())) {
				createCriteria2.andIdNotEqualTo(gradeDO.getId());
			}
			example.or(createCriteria2);
		}
		
		int count = 0;
		try {
			count = memberGradeMapper.countByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(ResultDO.QUERY_ERROR_MSG + " 会员等级查重失败");
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, count > 0 ? 1 : 0);
		
		return result;
	}

	@Override
	public ResultDO judgeLevel(Integer integral) {
		ResultDO result = new ResultSupport(true);
		if (integral == null) {
			result.setSuccess(false);
			result.setErrorMsg("参数错误，积分不能为空");
			logger.error(ResultDO.PARAM_ERROR_MSG + " 积分不能为空");
			return result;
		}
		SysMemberGradeExample example = new SysMemberGradeExample();
		SysMemberGradeExample.Criteria criteria = example.createCriteria();
		criteria.andIntegralLessThanOrEqualTo(integral);
		criteria.andIsDeleteEqualTo(1);
		example.setOrderByClause(" integral desc ");
		example.setLimitByPage(" 1 ");
		List<SysMemberGrade> list = memberGradeMapper.selectByExample(example);

		if (list == null || list.size() == 0) {
			result.setSuccess(false);
			result.setErrorMsg("会员等级查询为空");
			logger.warn("会员等级查询为空");
			return result;
		}
		MemberGradeDO memberGradeDO = getMemberGradeDO(list.get(0));
		result.setModel(ResultDO.FIRST_MODEL_KEY, memberGradeDO);
		return result;
	}

}
